package codeRandomThoughts.Test222完全二叉树中的节点个数;

import codeRandomThoughts.utils.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    public int countNodes(TreeNode root) {
        TreeNode node = root;
        int count=0;
        if (node == null){
            return count;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()){
            int size = queue.size();
            while (size>0){
                node = queue.poll();
                count++;

                if (node.left!=null)
                    queue.offer(node.left);
                if (node.right!=null)
                    queue.offer(node.right);
                size--;
            }
        }
        return count;
    }
}
